package cn.iocoder.yudao.module.pricehistory.controller.admin.pricehistory1;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pricehistory.controller.admin.pricehistory1.vo.*;
import cn.iocoder.yudao.module.pricehistory.dal.dataobject.pricehistory1.PriceHistoryDO;
import cn.iocoder.yudao.module.pricehistory.service.pricehistory1.PriceHistoryService;

@Tag(name = "管理后台 - 记录旅游线路价格的变动历史")
@RestController
@RequestMapping("/pricehistory/price-history")
@Validated
public class PriceHistoryController {

    @Resource
    private PriceHistoryService priceHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建记录旅游线路价格的变动历史")
    @PreAuthorize("@ss.hasPermission('pricehistory:price-history:create')")
    public CommonResult<Long> createPriceHistory(@Valid @RequestBody PriceHistorySaveReqVO createReqVO) {
        return success(priceHistoryService.createPriceHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新记录旅游线路价格的变动历史")
    @PreAuthorize("@ss.hasPermission('pricehistory:price-history:update')")
    public CommonResult<Boolean> updatePriceHistory(@Valid @RequestBody PriceHistorySaveReqVO updateReqVO) {
        priceHistoryService.updatePriceHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除记录旅游线路价格的变动历史")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pricehistory:price-history:delete')")
    public CommonResult<Boolean> deletePriceHistory(@RequestParam("id") Long id) {
        priceHistoryService.deletePriceHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得记录旅游线路价格的变动历史")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pricehistory:price-history:query')")
    public CommonResult<PriceHistoryRespVO> getPriceHistory(@RequestParam("id") Long id) {
        PriceHistoryDO priceHistory = priceHistoryService.getPriceHistory(id);
        return success(BeanUtils.toBean(priceHistory, PriceHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得记录旅游线路价格的变动历史分页")
    @PreAuthorize("@ss.hasPermission('pricehistory:price-history:query')")
    public CommonResult<PageResult<PriceHistoryRespVO>> getPriceHistoryPage(@Valid PriceHistoryPageReqVO pageReqVO) {
        PageResult<PriceHistoryDO> pageResult = priceHistoryService.getPriceHistoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PriceHistoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出记录旅游线路价格的变动历史 Excel")
    @PreAuthorize("@ss.hasPermission('pricehistory:price-history:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPriceHistoryExcel(@Valid PriceHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PriceHistoryDO> list = priceHistoryService.getPriceHistoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "记录旅游线路价格的变动历史.xls", "数据", PriceHistoryRespVO.class,
                        BeanUtils.toBean(list, PriceHistoryRespVO.class));
    }

}